package com.atguigu.service.impl;

import com.atguigu.dao.BaseDao;
import com.atguigu.dao.PermissionDao;
import com.atguigu.dao.RolePermissionDao;
import com.atguigu.entity.Permission;

import com.atguigu.service.PermissionService;
import com.atguigu.util.PermissionHelper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class PermissionServiceImpl extends BaseServiceImpl<Permission> implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private RolePermissionDao rolePermissionDao;

    @Override
    public List<Map<String, Object>> findZnodes(Long roleId) {
        List<Permission> permissionList = permissionDao.findAll();
        List<Long> permissionIdByRoleId = rolePermissionDao.findPermissionIdByRoleId(roleId);
        List<Map<String, Object>> zNodes = new ArrayList<>();
        for (Permission permission : permissionList) {
            Map<String, Object> map = new HashMap();
            map.put("id", permission.getId());
            map.put("pId", permission.getParentId());
            map.put("name", permission.getName());
            if (permissionIdByRoleId.contains(permission.getId())) {
                map.put("checked", true);
            }
            zNodes.add(map);
        }
        return zNodes;
    }

    @Override
    public List<Permission> findPermissionByAdminId(Long adminId) {
        List<Permission> permissionList = null;
        if (adminId == 1) {
            permissionList = permissionDao.findAll();
        } else {
            permissionList = permissionDao.findPermissionByAdminId(adminId);
        }
        List<Permission> permissionList1 = PermissionHelper.bulid(permissionList);
        return permissionList1;
    }

    @Override
    public List<Permission> findAll() {
        List<Permission> permissionList = permissionDao.findAll();
        List<Permission> permissionList1 = PermissionHelper.bulid(permissionList);
        return permissionList1;
    }

    @Override
    public List<String> findCodeByAdminId(Long adminId) {
        List<String> codes = null;
        if (adminId == 1) {
            codes = permissionDao.findAllCode();
        } else {
            codes = permissionDao.findCodeByAdminId(adminId);
        }

        return codes;
    }

    @Override
    public BaseDao<Permission> getBaseDao() {
        return permissionDao;
    }

    @Override
    public void delete(Serializable id) {
        List<Permission> permissionList = permissionDao.findPermissionByParentID(id);
        if (permissionList != null && permissionList.size() > 0) {
            for (Permission permission : permissionList) {
                List<Permission> permissionList1 = permissionDao.findPermissionByParentID(permission.getParentId());
                if (permissionList1 != null && permissionList1.size() > 0) {
                    for (Permission permission1 : permissionList1) {
                        permissionDao.delete(permission1.getId());
                    }
                }
                permissionDao.delete(permission.getId());
            }
        }

        permissionDao.delete(id);

    }
}
